<?php
class ProductsController extends AppController {
	var $name = 'Products';
	var $components = array('Uploader.Uploader');
	var $errors = array();
	//var $uses=array('Image');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/product/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '2M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
		$this->Auth->allow('pages','chitiet','cateproduct','catenews');
	}
	function index($fid=NULL) {
		$this->Product->recursive = 1;
		$conds = array('Product.status' =>2,'Product.lang'=>$this->lg);
		if(!empty($fid)){
		$conds = array('Product.status' =>2,'Product.lang'=>$this->lg,'Product.cateproducts_id'=>$fid);
		}
		$this->paginate = array(
			'conditions'=>$conds,
			'limit'=>5,
			'order'=>'Product.id DESC'
		);
		$this->set('products', $this->paginate());
		//debug($this->paginate());
	}

	function chitiet($id = null) {
		#$thiss->layout = false;
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('Product.status'=>2);
		$product = $this->Product->read(null, $id);
		$cid = $product['Product']['cateproducts_id'];
		$this->set('product',$product);
		$otherproduct = $this->Product->find('all',array('conditions'=>array('Product.id <>'=>$id,'Product.status'=>2,'cateproducts_id'=>$cid,'Product.lang'=>$this->lg),'limit'=>6,'order'=>'Product.id DESC'));
		$this->set(compact('otherproduct'));
		//debug($product);
	}

	function admin_index() {
		$this->layout='admin';
		$this->Product->recursive = 1;
		$conditions = '';
		$cate = $this->data['Product']['cateproducts'];
		$key = $this->data['Product']['key'];
		if($key!= null){
			$conditions = array('Product.title LIKE '=>'%'.$key.'%');
		}
		elseif($cate != null){
			$conditions = array('Product.cateproducts_id' => $cate);
		}
		elseif($cate != null && $key!= null){
			$conditions = array( 'Product.title LIKE '=>'%'.$key.'%');
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Product.id DESC'
		);
		$this->set('products', $this->paginate());
		//debug($this->paginate);
		$cateproducts = $this->Product->Cateproduct->find('list');
		$this->set(compact('cateproducts','cate','key'));
	}


	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại sản phẩm này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('products', $this->Product->read(null, $id));
		//debug($this->Product->read(null, $id));
	}

	function admin_add() {
		//debug($this->data);
		if (!empty($this->data)) {
			$filename = $this->Uploader->upload('image');
			//debug($filename);
			$this->Product->create();
			//$users_id = $this->Auth->user('id');
			$this->data['Product']['image']  = $filename['name'];
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		$cateproducts = $this->Product->Cateproduct->find('list');
		$this->set(compact('cateproducts'));
	}

	 function admin_edit($id = null) {
		$files = $this->Product->find('list',array(
			'conditions'=>array('Product.id'=>$id),
			'fields'=>'image')
		);
		$file ='';
		$file = "files" . DS . "uploads/product" . DS .$files[$id];
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Product']['image'] = $files[$id];
            if ($data = $this->Uploader->upload('image')) {
				$tmpname  = $data['name'];
				if(is_file($file)){
					unlink($file);
				}
				$this->data['Product']['image'] = $tmpname;
            }
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
		$cateproducts = $this->Product->Cateproduct->find('list');
		$this->set(compact('cateproducts'));
		//$this->set('model',$this->uses[0]);
		//debug($this->data);
	}

	function admin_delimg($id = null){
			$files = $this->Product->find('list',array(
				'conditions'=>array('Product.id'=>$id),
				'fields'=>'image')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "product" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
			$this->Product->query("Update tblproducts set image = '' where id='".$id."'");
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này !', true));
			$this->redirect(array('action'=>'index'));
		}
		$documents = $this->Product->Image->find('count',array('conditions'=>array('Image.products_id'=>$id)));
		if($documents >0){
			$this->Session->setFlash(__('Sản phẩm này có chứa bản ghi con. Hãy xóa các bản ghi con trước !', true));
			$this->redirect(array('action' => 'index'));
		}else{
			if($id){
				$files = $this->Product->find('first',array(
				'conditions'=>array('Product.id'=>$id),
				'fields'=>'image')
				);
				$file = "files/uploads/product/".$files['Product']['image'];
					if(is_file($file)){
						unlink($file);
					}
			}
			if ($this->Product->delete($id)) {
				$this->Session->setFlash(__('Đã xóa !', true));
				$this->redirect(array('action'=>'index'));
			}
		}
		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được bài viết !', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được bài viết !', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
			if($item != null){
				$files = $this->Product->find('first',array(
				'conditions'=>array('Product.id'=>$item),
				'fields'=>'image')
				);
				$file = "files/uploads/product/".$files['Product']['image'];
				if(is_file($file)){
					unlink($file);
				}
			}
    		$this->Product->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index'));
    }
	function delfile($id = null){
		$files = $this->Product->find('first',array(
			'conditions'=>array('Product.id'=>$item),
			'fields'=>'image')
			);
		$file = "files/uploads/product/".$files['Product']['image'];
		if(is_file($file)){
			unlink($file);
		}
		$this->Product->query("Update tblproducts set image = '' where id='".$id."'");
    }

}
?>